//
//  RootViewController.m
//  cmSqlDemo2
//
//  Created by Chaiyasit Tayabovorn on 5/8/11.
//  Copyright 2011 CodeMobiles Co.,Ltd. All rights reserved.
//

#import "exerciseOViewController.h"
//#import "CMDatabaseManager.h"


@implementation exerciseOViewController
@synthesize foods,ExerciseOViewController;
//@synthesize dataFromDatabase;
//@synthesize dataFromDatabase;


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    appDelegate = (CMNavBarAppDelegate *)[UIApplication sharedApplication].delegate;
    
    if (appDelegate.isDBReady) {
        self.foods  = [appDelegate getProjectsEO];
    }
    
    
    self.title = @"Exercise O";
    // Set up the Edit Button.
    //self.navigationItem.leftBarButtonItem = self.editButtonItem;//
    
    // Set up the Add Button.
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)];//กดปุมนี้แล้วไปไหน ขื่อ method
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];
    
    //    [CMDatabaseManager init];
    // self.dataFromDatabase = [CMDatabaseManager query];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */


// Customize the number of sections in the table view. call section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section //
{
    return [self.foods count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];// load cell 
    if (cell == nil) {//เป็นว่าง
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    food *testfoods = (food *)[self.foods objectAtIndex:indexPath.row];
    //    food *testcal = testfoods.calorie;
    cell.textLabel.text = testfoods.nameE;
    NSString *s1= testfoods.calorieE;
    NSString *s2= @"cal";
    NSString *newString;
    newString = [s1 stringByAppendingString:s2];
    //cell.detailTextLabel.text = testfoods.calorie;
    //cell.detailTextLabel.text = @"cal";
    cell.detailTextLabel.text = newString;   
    // Configure the cell. เฉพาะ rext
    // cell.textLabel.text = [dataFromDatabase objectAtIndex:indexPath.row];
    
    // currently not used in this workshop
    // cell.detailTextLabel.text = @"";
    // cell.imageView.image = nil;
    return cell;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath// delete
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // delete record at inbound index
        //[CMDatabaseManager delete:[self.dataFromDatabase objectAtIndex:indexPath.row]];
        // reload data again
        // self.dataFromDatabase = [CMDatabaseManager query];
        //[self.tableView reloadData];
    }
}
#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (cell.accessoryType==UITableViewCellAccessoryCheckmark) {
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self dismissModalViewControllerAnimated:YES];
    }else{
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self dismissModalViewControllerAnimated:YES];
    }// Nothing need here in this workshop
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc
{
    //[dataFromDatabase release];
    [super dealloc];
}


- (void)insertNewObject
{
//    NSString *timeString = [NSString stringWithFormat:@"%@", [NSDate date]];
//    [CMDatabaseManager insert:timeString];
    //self.dataFromDatabase = [CMDatabaseManager query];
    //[self.tableView reloadData];
}

@end
